from keras.datasets import imdb
import numpy as np

def vectorize_seqs(seqs, dimensions=10000):
    results = np.zeros((len(seqs), dimensions))
    for i, seq in enumerate(seqs):
        results[i, seq] = 1
    return results 


def decode_review(index_seq):
    word_index = imdb.get_word_index()
    reverse_word_index = dict(
        [(value, key) for (key, value) in word_index.items()]
    )
    decoded_review = ' '.join(
        [reverse_word_index.get(i-3, '?') for i in index_seq]
    )
    print(decoded_review)

# 数据保存在imdb.npz中，一种numpy压缩数据格式
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

decode_review(train_data[0])